package com.juzhencms.apps.common.tread;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.juzhencms.apps.base.busi.mapper.AppWeixinSourceMapper.AppWeixinSourceListParam;
import com.juzhencms.apps.busi.service.AppWeixinSourceService;
import com.juzhencms.apps.busi.service.CommonService;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class TreadTasks  extends CommonService{
	@Autowired
	private AppWeixinSourceService appWeixinSourceService;
	
    @Async
    public void startUpdateWeixinNewsSource(int newsID,boolean deleteEvent)  {
    	log.info("startUpdateWeixinNewsSource===newsID------："+newsID);
    	if(!deleteEvent) {//更新
    		//查找此条新闻的数据
        	AppWeixinSourceListParam param = new AppWeixinSourceListParam();
            param.setNewsID(","+newsID+",");
        	List<HashMap> sourceList = this.getAppWeixinSourceMapper().listSource(param);
        	log.info(sourceList+"===startUpdateWeixinNewsSource==sourceList");
        	if(sourceList!=null && sourceList.size()>0) {
        		for(int i=0;i<sourceList.size();i++) {
        			HashMap row=sourceList.get(i);
        			//更新数据
        			this.appWeixinSourceService.updateWeixinNewsSource(newsID, row, null);        			
        		}
        	}
    	}else { //删除
    		
    		//查找此条新闻的数据（这里只超出只包含这一条新闻的素材并删除，多新闻素材不会被删除，因为删除后可能存在其他问题）
        	AppWeixinSourceListParam param = new AppWeixinSourceListParam();
            //param.setNewsIDStr(","+newsID+",");            
            param.setNewsID(","+newsID+","); //删除和此条文章有关的所有的素材
        	List<HashMap> sourceList = this.getAppWeixinSourceMapper().listSource(param);
        	if(sourceList!=null && sourceList.size()>0) {
        		for(int i=0;i<sourceList.size();i++) {
        			HashMap row=sourceList.get(i);
        			//删除数据
        			this.appWeixinSourceService.deleteWeixinNewsSource(row);
        		}
        	}
    	}
    	
		/* System.out.println("startUpdateWeixinNewsSource");
		int i=0;
		while(i>=0) {
			i++;
			log.info("i======"+i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} //暂停，每一秒输出一次
		}*/
    }
    
    
}
